/**
 * Created by wuji on 2016/11/2.
 */
require("easyui");
require('layout/util');
var $alert = require('libs/alert');
$(function () {
    $("#detail_back").on("click",function () {
        window.history.go(-1);
    });
    $("#back").on("click",function () {
        window.history.go(-1);
    });
    init();//初始化控件
});

function  init() {
    queryPlan();
}


//查询数据
function queryPlan() {
    var id =$.getUrlParam('id');
    $.ajax({
        url:'api/v1/p/mms/logisticsChannelPlan/detail',
        type:'post',
        data:{
            id:id
        },
        dataType:'json',
        success:function (result) {
            if(result.code == 200){
                appendDate(result.data);
                // $('#goodsAdd_form input[name=code]').rules('remove',"remote");
            }else{
                $alert(result.message);
            }
        },
        error:function (e) {
            $alert('加载数据失败！');
        }
    })
}

//添加数据
function appendDate(data) {
    //物流产品 select添加数据
    appendOption('api/v1/p/mms/logistics/query',{},$("#logisticsPlanForm select[name=logisticsId]"),data.logisticsId);
    //物流线路select添加数据
    appendOption('api/v1/p/mms/logisticsChannel/query',{logisticsId:data.logisticsId},$("#logisticsPlanForm select[name=channelId]"),data.channelId);

    $("#logisticsPlanForm input[name=id]").val(data.id);
    $("#logisticsPlanForm input[name=startPort]").val(data.startPort);
    $("#logisticsPlanForm input[name=transferPort]").val(data.transferPort);
    $("#logisticsPlanForm input[name=destPort]").val(data.destPort);
    $("#logisticsPlanForm input[name=actualCarrier]").val(data.actualCarrier );
    $("#logisticsPlanForm input[name=voyageNo]").val(data.voyageNo);
    $("#logisticsPlanForm input[name=estimateLeaveDt]").val(data.estimateLeaveDt );
    $("#logisticsPlanForm input[name=estimateArriveDt]").val(data.estimateArriveDt );
    $("#logisticsPlanForm input[name=tLastOrderDt]").val(data.tLastOrderDt );
    $("#logisticsPlanForm input[name=tLastCollectDt ]").val(data.tLastCollectDt );
    $("#logisticsPlanForm input[name=lastOrderDt ]").val(data.lastOrderDt );
    $("#logisticsPlanForm input[name=lastCollectDt ]").val(data.lastCollectDt );
    $("#logisticsPlanForm input[name=memo ]").val(data.memo );

}
//添加参数
function appendOption(url,data,$select,value) {
    $.ajax({
        url:url,
        type:'post',
        data:data,
        dataType:'json',
        success:function (result) {
            if(result.code==200){
                var options =  '<option value="">请选择</option>';
                $.each(result.data,function (index,obj) {
                    var checked = "";
                    if(value){
                        if(value ==obj.warehouseId|| value ==obj.logisticsId|| value ==obj.id){
                            checked = "selected"
                        }
                    }
                    if(obj.warehouseId !=undefined){
                        options += "<option value='" + obj.warehouseId + "' "+checked+">" + obj.warehouseName + "</option>";
                    }else if(obj.id!=undefined){
                        if(obj.name != undefined){
                            options += "<option value='" + obj.id + "' "+checked+">" + obj.name + "</option>";
                        }else{
                            options += "<option value='" + obj.id + "' "+checked+">" + obj.nameCn  + "</option>";
                        }
                    }else {
                        options += "<option value='" + obj.logisticsId  + "' "+checked+">" + obj.name + "</option>";
                    }
                });
                $select.empty();
                $select.append(options);
            }else{
                var span = '<span  class="error-info"><span class="glyphicon glyphicon-remove"></span>'+
                    result.data.message+'</span>';
                $select.parent('div').next('div.col-xs-3').append(span);
            }
        },
        error:function (e) {
            console.error(url+'接口错误');
        }
    })
}